<?php


defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.event.plugin');
jckimport('ckeditor.htmlwriter.javascript');


class plgEditorPluginOverrides extends JPlugin 
{
		
  	function plgEditorClient(& $subject, $config) 
	{
		parent::__construct($subject, $config);
	}

	function beforeLoad(&$params)
	{
		
		$db =& JFactory::getDBO();
		
		$query = 'SELECT * FROM #__jckplugins WHERE published = 1';
	
		$db->setQuery( $query );
		$plugins = $db->loadObjectList();
		
		if (!is_array($plugins)) {
			JError::raiseWarning(100, $db->getErrorMsg() );
		}
		
		if(empty($plugins))
			return;
		
		//lets create JS object
		$javascript = new JCKJavascript();
		$script = "CKEDITOR.jckplugins = {";
		
		foreach($plugins as $plugin)
		{
			if(empty($plugin->params))
				continue;
		
			$params = new JParameter($plugin->params,JPATH_PLUGINS.DS.'editors'.DS.'jckeditor'.DS.'plugins'.DS.$plugin->name.DS.$plugin->name.'.xml');
			$name = $plugin->name;
			
			$title = $params->get('dialogtitle','false');
			$height = $params->get('height','false');
			$width = $params->get('width','false');
			$resizable = $params->get('resizable','false');
			
			//lets get plugin Joomla configurable options
						
			$options = false;
					
			if (isset($params->_xml['options'])) 
			{
				foreach ($params->_xml['options']->children() as $node)  
				{
					$key = $node->attributes('name');
					$default = $node->attributes('default');
					$value = $params->get($key,$default);
					if(is_numeric($value))
						$options .= "\"$key=$value\",";
					else
						$options .= "\"$key='".$value."'\",";
				}
			}
			
			if($options)
			{
				$options = substr($options, 0, -1);
				$options = '[' . $options  . ']';
				
			}	
			else
				$options = 'false';
			
			$script .= "$name:{'title':'$title','height':'$height','width':'$width','resizable':$resizable,'options':$options},";

		}
		if($script != "CKEDITOR.jckplugins = {")
			$script = substr($script, 0, -1);
		$script .= "};" . chr(13);
		
		$actionscript = "
		CKEDITOR.on( 'dialogDefinition', function( ev )
		{
			// Take the dialog name and its definition from the event
			// data.
			var dialogName = ev.data.name;
			var dialogDefinition = ev.data.definition;
			if(CKEDITOR.jckplugins[dialogName ])
			{
				var jckplugin = CKEDITOR.jckplugins[dialogName ];
				
				if(jckplugin.title) dialogDefinition.title = jckplugin.title;
				if(jckplugin.height) dialogDefinition.minHeight = jckplugin.height;
				if(jckplugin.width) dialogDefinition.minWidth = jckplugin.width;
				if(jckplugin.resizable !== false) dialogDefinition.resizable = jckplugin.resizable;
				
							
				
				if(jckplugin.options)
				{
					for(var k = 0; k < jckplugin.options.length;k++)
					{
						eval('CKEDITOR.config.' + jckplugin.options[k]);
					}
					
				}
			
			}
	 
			
		});

		for(var m in CKEDITOR.jckplugins)
		{  
			var jckplugin = CKEDITOR.jckplugins[m];
			
			if(jckplugin.options)
			{
				if(jckplugin.title != 'false') 	continue;
				for(var n = 0; n < jckplugin.options.length;n++)
				{
					eval('CKEDITOR.config.' + jckplugin.options[n]);
				}
				
			}
		}
		";

	
		
		$javascript->addScriptDeclaration($script.$actionscript);
		return $javascript->toRaw();
		
		
	}
	
	
	
	function afterLoad(&$params)
	{

		$javascript = new JCKJavascript();
		
		$script = "for(var m in CKEDITOR.jckplugins)
		{  
			var jckplugin = CKEDITOR.jckplugins[m];
			
			if(jckplugin.options)
			{
				if(jckplugin.title != 'false') 	continue;
				for(var n = 0; n < jckplugin.options.length;n++)
				{
					eval('CKEDITOR.config.' + jckplugin.options[n]);
				}
				
			}
		}";
			
		$javascript->addScriptDeclaration($script);
		return $javascript->toRaw();	

	}

}
